﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using DevExpress.XtraEditors;

namespace PRE.Catalog
{
    public partial class FrmCapnhatchucvu : PRE.Catalog.FrmBase
    {
        #region Variable
        DAL.EntitiesDAL.ChucvuDAL _dal = new DAL.EntitiesDAL.ChucvuDAL();
        BLL.EntitiesBLL.ChucvuBLL _bll = new BLL.EntitiesBLL.ChucvuBLL();

        DataTable _dtb = new DataTable();
        #endregion

        #region Inits
        public FrmCapnhatchucvu()
        {
            InitializeComponent();
        }
        #endregion

        #region Override
        protected override void PerformAdd()
        {
            ReadOnlyControl(false);
            ClearDataBindings();
            ChangeStatus(false);

            txt_Machucvu.Focus();

            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var id1 = grvChucvu.GetFocusedRowCellValue("MACHUCVU") + "";

            DialogResult result;
            var ok = _dal.Delete(id1);
            result = XtraMessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    XtraMessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            txt_Machucvu.Properties.ReadOnly = true;
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            ResetText();

            bto_Search.Visible = false;

            if (_dtb != null)
            {
               
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }
            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {

            var name_Tenchucvu = Proper(txt_Tenchucvu.Text);
            if (IsAdd)
            {
                var o = new DAL.Entities.Chucvu()
                {
                    MACHUCVU = Convert.ToString(txt_Machucvu.Text),
                    TENCHUCVU = name_Tenchucvu

                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                  
                }
                else
                {
                    XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            if (IsEdit)
            {
                var id1 = grvChucvu.GetFocusedRowCellValue("MACHUCVU") + "";
                var o = new DAL.Entities.Chucvu()
                {
                    MACHUCVU = Convert.ToString(txt_Machucvu.Text),
                    TENCHUCVU = name_Tenchucvu

                };

                var oki = _dal.Update(o);

                if (oki)
                {
                    XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txt_Machucvu.ResetText();
            txt_Tenchucvu.ResetText();


            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txt_Machucvu.DataBindings.Clear();
            txt_Tenchucvu.DataBindings.Clear();


            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {

            txt_Machucvu.DataBindings.Add("Text", _dtb, ".MACHUCVU");
            txt_Tenchucvu.DataBindings.Add("Text", _dtb, ".TENCHUCVU");


            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            txt_Machucvu.Properties.ReadOnly = isReadOnly;
            txt_Tenchucvu.Properties.ReadOnly = isReadOnly;
            grcChucvu.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            ChangeStatus(false);
            ReadOnlyControl(false);
            //ResetText();
            bto_Search.Visible = true;
            grcChucvu.Enabled = true;
            txt_Machucvu.Focus();

            base.PerformSearch();
        }

        /// <summary>
        /// Execute Search data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bto_Search_Click_1(object sender, EventArgs e)
        {        
            var name_Tenchucvu = Proper(txt_Tenchucvu.Text);
            var o = new DAL.Entities.Chucvu()
            {
                MACHUCVU = Convert.ToString(txt_Machucvu.Text),
                TENCHUCVU = name_Tenchucvu

            };

            grcChucvu.DataSource = _dal.Search(o);
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        protected override void LoadData()
        {
            try
            {
                _dtb = _dal.Select();

                if (_dtb != null)
                {
                    grcChucvu.DataSource = _dtb;

                    AppGridView.SummaryField(grvChucvu, "MACHUCVU", "Tổng = {0:#,0} Chức vụ", DevExpress.Data.SummaryItemType.Count);

                    grvChucvu.OptionsView.ColumnAutoWidth = true;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("Lỗi : " + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.LoadData();
        }

        #endregion

        #region Form Load
        private void grcChucvu_Load(object sender, EventArgs e)
        {
            PerformRefresh();
        }
        #endregion
       
    }
}
